<template>
  <div class="goosinfo-container">
    <!--商品轮播图页面-->
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <swiper :lunbotuList="lunbolist" :isfull="false"></swiper>
        </div>
      </div>
    </div>

    <!--商品购买区域-->
    <div class="mui-card">
      <div class="mui-card-header">{{goodsinfo.title}}</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p class="price">
            市场价：
            <del>￥{{goodsinfo.market_price}}</del>&nbsp;&nbsp;销售价：
            <span class="now_price">￥{{goodsinfo.sell_price}}</span>
          </p>
          <p>
            购买数量：
            <numbox @getcount="getSelectCount"></numbox>
          </p>
          <p>
            <mt-button type="primary" size="small">立即购买</mt-button>
            <mt-button type="danger" size="small" @click="addToShopCar">加入购物车</mt-button>
          </p>
        </div>
      </div>
    </div>
    <!--商品详情页面-->
    <div class="mui-card">
      <div class="mui-card-header">商品参数</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>商品货号：{{goodsinfo.goods_no}}</p>
          <p>库存情况：{{goodsinfo.stock_quantity}}</p>
          <p>上架时间：{{goodsinfo.add_time|dateFormat}}</p>
        </div>
      </div>
      <div class="mui-card-footer">
        <mt-button type="primary" size="large" plain @click="goDesc(id)">图文介绍</mt-button>
        <mt-button type="danger" size="large" plain @click="goComment(id)">商品评论</mt-button>
      </div>
    </div>
  </div>
</template>
<script>
import swiper from "../subcomponents/swiper";
import numbox from "../subcomponents/goodsinfo_numbox";
export default {
  data() {
    return {
      id: this.$route.params.id,
      lunbolist: [],
      goodsinfo: {},
      selectcount:1
    };
  },
  created() {
    this.getLunbo();
    this.getGoodsInfo();
  },
  methods: {
    async getLunbo() {
      const res = await this.$http.get("/getthumimages/" + this.id);
      //先循环轮播图数组的每一项，为item添加img属性，因为轮播图只认识img不认识src
      res.data.message.forEach(item => {
        item.img = item.src;
      });
      this.lunbolist = res.data.message;
    },
    async getGoodsInfo() {
      const res = await this.$http.get("goods/getinfo/" + this.id);
      this.goodsinfo = res.data.message[0];
    },
    goDesc(id) {
      this.$router.push({ name: "goodsdesc", params: { id } });
    },
    goComment(id) {
      this.$router.push({ name: "goodscomment", params: { id } });
    },
    getSelectCount(count){
      this.selectcount = count;
    },
    addToShopCar(){
      var goodsinfos = {
        id:this.id,
        count:this.selectcount,
        price:this.goodsinfo.sell_price,
        selected:true,
        };
        this.$store.commit('addToCar',goodsinfos);
    }
  },
  components: {
    swiper,
    numbox
  }
};
</script>
<style lang="scss" scoped>
.goosinfo-container {
  background-color: #eee;
  overflow: hidden;
  .now_price {
    color: red;
    font-size: 16px;
    font-weight: bold;
  }
  .mui-card-footer {
    display: block;
    button {
      margin: 10px 0;
    }
  }
}
</style>
